package com.wshl.aijiuunion.controller.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.SPUtils;
import com.lzy.okgo.OkGo;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.wshl.aijiuunion.BaseActivity;
import com.wshl.aijiuunion.GlobConstant;
import com.wshl.aijiuunion.R;
import com.wshl.aijiuunion.callback.RequestCallback;
import com.wshl.aijiuunion.model.RequestModel;
import com.wshl.aijiuunion.view.adapter.FujinStoreAdapter;
import com.wshl.aijiuunion.view.bean.FujinStoreBean;
import com.wshl.aijiuunion.view.bean.StorelistBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created buy 风凊扬 on 2019/11/9 0009
 */
public class StoreListActivity extends BaseActivity implements OnRefreshLoadMoreListener {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.recycle_store)
    RecyclerView recycleStore;
    @BindView(R.id.refresh_store)
    SmartRefreshLayout refreshStore;

    private RequestModel model;
    private int page = 1;
    private FujinStoreAdapter fujinStoreAdapter;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_storelist;
    }

    private List<FujinStoreBean> fujinStoreBeanList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);

        model = new RequestModel();
        tvTopTitle.setText("附近的店");
        recycleStore.setLayoutManager(new LinearLayoutManager(this));
        refreshStore.setOnRefreshLoadMoreListener(this);
        getStorelist(false);
    }

    private void getStorelist(final boolean isLoadmore) {

        String lat = SPUtils.getInstance().getString(GlobConstant.LATITUDE);
        String lng = SPUtils.getInstance().getString(GlobConstant.LNGTITUDE);
        model.storeList(this, page, lat, lng, new RequestCallback() {
            @Override
            public void onSuccess(String response) {

                StorelistBean storelistBean = GsonUtils.fromJson(response, StorelistBean.class);
                StorelistBean.DataBeanX data = storelistBean.getData();
                List<StorelistBean.DataBeanX.DataBean> data1 = data.getData();

                if (data1 != null && data1.size() > 0) {

                    if (fujinStoreBeanList == null) {

                        fujinStoreBeanList = new ArrayList<>();
                        for (StorelistBean.DataBeanX.DataBean dataBean : data1) {
                            fujinStoreBeanList.add(new FujinStoreBean((ArrayList<String>) dataBean.getImages(), dataBean.getAddress(), dataBean.getDistance(), dataBean.getId(), dataBean.getName(), dataBean.getPhone(), dataBean.getThumb(), dataBean.getLat(), dataBean.getLng(), dataBean.getOpen_time()));
                        }
                        fujinStoreAdapter = new FujinStoreAdapter(recycleStore);
                        fujinStoreAdapter.setData(fujinStoreBeanList);
                        recycleStore.setAdapter(fujinStoreAdapter);
                    } else if (isLoadmore) {

                        for (StorelistBean.DataBeanX.DataBean dataBean : data1) {
                            fujinStoreBeanList.add(new FujinStoreBean((ArrayList<String>) dataBean.getImages(), dataBean.getAddress(), dataBean.getDistance(), dataBean.getId(), dataBean.getName(), dataBean.getPhone(), dataBean.getThumb(), dataBean.getLat(), dataBean.getLng(), dataBean.getOpen_time()));
                        }
                        fujinStoreAdapter.notifyDataSetChanged();
                        refreshStore.finishLoadMore();
                    } else {
                        fujinStoreBeanList.clear();
                        for (StorelistBean.DataBeanX.DataBean dataBean : data1) {
                            fujinStoreBeanList.add(new FujinStoreBean((ArrayList<String>) dataBean.getImages(),dataBean.getAddress(), dataBean.getDistance(), dataBean.getId(), dataBean.getName(), dataBean.getPhone(), dataBean.getThumb(), dataBean.getLat(), dataBean.getLng(), dataBean.getOpen_time()));
                        }
                        fujinStoreAdapter.notifyDataSetChanged();
                        refreshStore.finishRefresh();
                    }

                } else {
                    refreshStore.finishRefresh();

                    if (isLoadmore) {

                        refreshStore.finishLoadMore();
                    }
                }


            }

            @Override
            public void onError(int code) {

            }

            @Override
            public void onIntentError() {

            }
        });
    }

    @OnClick(R.id.img_back)
    public void onViewClicked() {
        this.finish();
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

        page++;
        getStorelist(true);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {

        page = 1;
        getStorelist(false);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkGo.getInstance().cancelAll();
    }
}
